package com.example.musicrecommend.controller;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.musicrecommend.common.BaseResponse;
import com.example.musicrecommend.common.ErrorCode;
import com.example.musicrecommend.common.ResultUtils;
import com.example.musicrecommend.exception.BusinessException;
import com.example.musicrecommend.exception.ThrowUtils;
import com.example.musicrecommend.model.dto.paper.PaperAddRequest;
import com.example.musicrecommend.model.dto.paper.PaperDeleteRequest;
import com.example.musicrecommend.model.dto.paper.PaperQueryRequest;
import com.example.musicrecommend.model.dto.paper.PaperUpdateRequest;
import com.example.musicrecommend.model.entity.Paper;
import com.example.musicrecommend.model.entity.QuestionScore;
import com.example.musicrecommend.service.PaperService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/paper")
@Slf4j
public class PaperController {

    @Resource
    private PaperService paperService;

    /**
     * 添加卷子
     * @param paperAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addPaper(@RequestBody PaperAddRequest paperAddRequest, HttpServletRequest request) {
        if(paperAddRequest == null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperAddRequest,paper);
        List<QuestionScore> questions = paperAddRequest.getQuestions();
        if(questions !=null) {
            paper.setQuestions(JSONUtil.toJsonStr(questions));
        }
        // 校验
        paperService.validPaper(paper, true);
        boolean result = paperService.save(paper);
        ThrowUtils.throwIf(!result,ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(paper.getId());
    }


    /**
     * 删除卷子
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public  BaseResponse<Boolean> deletePaper(@RequestBody PaperDeleteRequest deleteRequest, HttpServletRequest request) {

        if (deleteRequest == null || deleteRequest.getIds() ==null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b= paperService.removePaper(deleteRequest.getIds());
        return ResultUtils.success(b);
    }

    /**
     * 更新试卷
     * @param paperUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updatePaper(@RequestBody PaperUpdateRequest paperUpdateRequest,HttpServletRequest request) {
        if (paperUpdateRequest == null || paperUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Paper paper = new Paper();
        BeanUtils.copyProperties(paperUpdateRequest, paper);
        List<QuestionScore> questions = paperUpdateRequest.getQuestions();
        if(questions!=null) {
            List<Long> list = questions.stream().map(QuestionScore::getId).collect(Collectors.toList());
            boolean flag = list.stream().distinct().count() < list.size();
            if(flag) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"不能添加重复题目");
            }
            paper.setQuestions(JSONUtil.toJsonStr(questions));
        }

        // 校验
        paperService.validPaper(paper, false);
        Paper oldPaper = paperService.getById(paper.getId());
        ThrowUtils.throwIf(oldPaper == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = paperService.updateById(paper);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }


    /**
     * 根据id获取卷子
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Paper> getPaperById(long id,HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Paper paper = paperService.getById(id);
        ThrowUtils.throwIf(paper == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(paper);
    }


    /**
     * 分页获取试卷列表
     * @param paperQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    public BaseResponse<Page<Paper>> listPaperByPage( PaperQueryRequest paperQueryRequest,
                                                     HttpServletRequest request) {
        long current = paperQueryRequest.getCurrent();
        long size = paperQueryRequest.getPageSize();
        Page<Paper> paperPage = paperService.page(new Page<>(current, size),
                paperService.getQueryWrapper(paperQueryRequest));
        return ResultUtils.success(paperPage);
    }

}
